<?php

//tailvh@gmail.com

//Require model files
require_once ROOT_APPLICATION_MODULES_DEFAULT_MODELS . DS . 'Product.php';
require_once ROOT_APPLICATION_MODULES_DEFAULT_MODELS . DS . 'Productoption.php';
require_once ROOT_APPLICATION_MODULES_DEFAULT_MODELS . DS . 'Productoptionvalue.php';
require_once ROOT_APPLICATION_MODULES_DEFAULT_MODELS . DS . 'Productoptionvalueoption.php';
require_once ROOT_APPLICATION_MODULES_DEFAULT_MODELS . DS . 'Productattribute.php';
//Require objects files
require_once ROOT_APPLICATION_MODULES_DEFAULT_OBJECTS . DS . 'Product.php';
require_once ROOT_APPLICATION_MODULES_DEFAULT_OBJECTS . DS . 'Productoptionvalue.php';
require_once ROOT_APPLICATION_MODULES_DEFAULT_OBJECTS . DS . 'Productoptionvalueoption.php';
require_once ROOT_APPLICATION_MODULES_DEFAULT_OBJECTS . DS . 'Productattribute.php';
//Require forms files
require_once ROOT_APPLICATION_MODULES_DEFAULT_FORMS . DS . 'Product.php';
require_once ROOT_APPLICATION_MODULES_DEFAULT_FORMS . DS . 'Productoptionvalue.php';
require_once ROOT_APPLICATION_MODULES_DEFAULT_FORMS . DS . 'Productaddinfo.php';

class ProductoptionvalueController extends Zend_Controller_Action {

    ///////////////////////
    // V A R I A B L E S //
    ///////////////////////

    private  $modelProduct;
    private  $modelProductoption;
    private  $modelProductoptionvalue; 
    private  $modelProductoptionvalueoption;
    private  $modelProductattribute;
    private $_redirector;
    
    /////////////////////
    // O V E R I D E S //
    /////////////////////

    public function preDispatch() {

    }

    public function init() {
        $layoutPath = APPLICATION_PATH . '/templates/default';
        $option = array('layout' => 'index',
        'layoutPath' => $layoutPath);
        Zend_Layout::startMvc($option);
        
        $this->modelProduct = new End_Models_Product(); 
        $this->modelProductoption = new End_Models_Productoption();
        $this->modelProductoptionvalue = new End_Models_Productoptionvalue();
        $this->modelProductoptionvalueoption = new End_Models_Productoptionvalueoption(); 
        $this->modelProductattribute = new End_Models_Productattribute();
        $this->_redirector = $this->_helper->getHelper('Redirector');
   }

    ///////////////////////
    // U T I L I T I E S //
    ///////////////////////
   
        
    ///////////////////
    // A C T I O N S //
    ///////////////////
    //INDEX ACTION
    public function indexAction() { 
        $tmpArrProductoptionvalue = $this->modelProductoptionvalue->fetchAll();
        $this->view->arrProductOptionValue = $tmpArrProductoptionvalue; 
    }

    public function addAction(){ 
        $arrProductOption = $this->modelProductoption->_list();
        $formProductoptionvalue = new Application_Form_Productoptionvalue();
  
        if ($this->getRequest()->isGet()) { 
            $formProductoptionvalue->product_option_value_id->setValue($Productoption_value_id);
        } else {
            $Cancel = $this->getRequest()->getPost('cancel', '0');
            if ($Cancel) {
                $this->_redirector->gotoUrl('productoptionvalue/index');
            }
            
            $formData = $this->getRequest()->getPost();
            if ($formProductoptionvalue->isValid($formData)) { 

                //production_option_value object
                $Productoptionvalue = new Productoptionvalue( null, $formData['product_option_value_name']);
                
                $id = $this->modelProductoptionvalue->_insert($Productoptionvalue);
               
                //production_option_option_value opject
                if(isset($formData['product_option_name']))
                foreach($formData['product_option_name'] as $option)
                {
                $Productoptionvalueoption = new Productoptionvalueoption(null,  $option, $id);
                
                $this->modelProductoptionvalueoption->_insert($Productoptionvalueoption);
                }
                
                
                $this->_redirector->gotoUrl('productoptionvalue/index');
            } else { 
                $formProductoptionvalue->populate($formData);
            }
        }
        $formProductoptionvalue->product_option_name->addMultiOptions($arrProductOption);
        $this->view->form = $formProductoptionvalue;
        
    }
    
    public function deleteAction(){
        $id = $this->_getParam('id','0');
        $Productoptionvalue = $this->modelProductoptionvalue->_find($id);
        if(isset($Productoptionvalue))
        {
            $this->modelProductoptionvalue->_delete($id);
        }
        
        $arrProductattribute = $this->modelProductattribute->_filter('product_option_value_id = ?', $id);
        if(isset($arrProductattribute))
            foreach($arrProductattribute as $Productattribute)
            {
                $this->modelProductattribute->_delete($Productattribute->product_attribute_id);
            }
            
        $arrProductoptionvalueoption = $this->modelProductoptionvalueoption->_filter('product_option_value_id = ?', $id);
        if(isset($arrProductoptionvalueoption))
            foreach($arrProductoptionvalueoption as $Productoptionvalueoption)
            {
                $this->modelProductoptionvalueoption->_delete($Productoptionvalueoption->product_option_value_option_id);
            }
            
        
        $this->_redirector->gotoUrl('productoptionvalue/index');
    }
    
    public function editAction(){
        $id = $this->_getParam('id','0');
        $Productoptionvalue = $this->modelProductoptionvalue->_find($id);                 
        $formProductoptionvalue = new Application_Form_Productoptionvalue();             
        $arrProductOption = $this->modelProductoption->_list();
        
        if (!$this->getRequest()->isGet())                                      
        {
            if(isset($Productoptionvalue))                                           
            {
                $formProductoptionvalue->product_option_value_id->setValue($id);

                $Cancel = $this->getRequest()->getPost('cancel', '0');          
                if ($Cancel) {
                    $this->_redirector->gotoUrl('productoptionvalue/index');         
                }

                $formData = $this->getRequest()->getPost();                    
                if ($formProductoptionvalue->isValid($formData)) { 
                    
                    $Productoptionvalue->product_option_value_name = $formData['product_option_value_name']; 
                    $this->modelProductoptionvalue->_update($Productoptionvalue); 
        
                    //delete axist relation
                    $arrDeleted = $this->modelProductoptionvalueoption->_filter('product_option_value_id = ?', $id);
                    if(isset($arrDeleted))
                        foreach($arrDeleted as $Deleted)
                        {   
                            $this->modelProductoptionvalueoption->_delete($Deleted->product_option_value_option_id);
                        }
                    //add new relation
                    if(isset($formData['product_option_name']))
                    foreach($formData['product_option_name'] as $option)
                    {
                        $Productoptionvalueoption = new Productoptionvalueoption(null,  $option, $id);
                        $this->modelProductoptionvalueoption->_insert($Productoptionvalueoption);
                    }  
                    
                    $this->_redirector->gotoUrl('productoptionvalue/index');         
                } else {             
                    
                    $formProductoptionvalue->populate($formData);                    
                }                                                              
            }                                                                   
            else
            {
                $this->_redirector->gotoUrl('productoptionvalue/index');             
                
            } 
            $formProductoptionvalue->product_option_name->addMultiOptions($arrProductOption);
            $this->view->form = $formProductoptionvalue;                             
        }
        else                                                                    
        {       
            if(isset($Productoptionvalue))                                           
            {
                $arrProductoptionvalueoption = $this->modelProductoptionvalueoption->_filter('product_option_value_id = ?', $id);
                if(isset($arrProductoptionvalueoption))
                    foreach($arrProductoptionvalueoption as $Productoptionvalueoption)
                    {
                        $arrProductoptionchecked[] = $Productoptionvalueoption->product_option_id;
                    } 
                
                $UC = new Utility_Converter();                                  
                $formProductoptionvalue->populate($UC->objectToArray($Productoptionvalue));   
                $formProductoptionvalue->product_option_name->addMultiOptions($arrProductOption);
                $formProductoptionvalue->product_option_name->setValue($arrProductoptionchecked);
           
                $this->view->form = $formProductoptionvalue;
            }
            else
            {
                $this->_redirector->gotoUrl('productoptionvalue/index');   
            }
        }
        
    }
    
}